 package com.stork.sms.activity;
import java.io.IOException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.HashMap;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.stork.sms.R;
import com.stork.sms.fragment.ForgetPasswordFragment;
import com.stork.sms.fragment.LoginPageFragment;
import com.stork.sms.service.schooldetails.Schooldetails;
import com.stork.sms.utilities.SharedPreference;

public class Login extends Activity {
	
	public static final String COLLEGE_NAME = "collegename";
	public static final String ADDRESS = "address";
	public static final String PHONE= "phone";
	public static final String EMAIL= "email";
	public static final String WEBSITE= "website";
	private static final String SOAP_ACTION = "http://tempuri.org/getschooldetails";
	private static final String METHOD_NAME = "getschooldetails";
	private static final String NAMESPACE = "http://tempuri.org/";
    private static final String URL = "http://10.0.2.2:2341/smsservice/webmethods.asmx";
    ArrayList<HashMap<String,Integer>>Itemlist;
	ActionBar ab;	
	Schooldetails schooldetails;
	TextView LoginTextview,ForgetPasswordTextview;
	Fragment ForgetpasswordFragment,LoginFragment;
	String name="Stork Software Systems Pvt Lmt",
			cadd="No 45,Pondy bazar",
			cphone="9894564152",
			cemail="hr@storksystem.com",
			cwebsite="click GoTo www.storksystems.com";	 
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		
//		ab=getActionBar();
//		ab.setDisplayHomeAsUpEnabled(true);
//		ab.setTitle("Login");
//		
		LoginTextview=(TextView)findViewById(R.id.logintextView1);
		ForgetPasswordTextview=(TextView)findViewById(R.id.forgetpasswordtextView2);
		
				
		SharedPreference.writeString(getApplicationContext(), COLLEGE_NAME, name);
		SharedPreference.writeString(getApplicationContext(), ADDRESS, cadd);
		SharedPreference.writeString(getApplicationContext(), PHONE, cphone);
		SharedPreference.writeString(getApplicationContext(), EMAIL, cemail);
		SharedPreference.writeString(getApplicationContext(), WEBSITE, cwebsite);
		
		LoginFragment=new LoginPageFragment();
		FragmentManager fm=getFragmentManager();
		FragmentTransaction ft=fm.beginTransaction().replace(R.id.fragment, LoginFragment);
		ft.commit();
		LoginTextview.isEnabled();
		LoginTextview.setBackgroundResource(R.color.none);
		ForgetPasswordTextview.setTextColor(Color.BLACK);
		ForgetPasswordTextview.setBackgroundResource(R.color.white);
		LoginTextview.setTextColor(Color.WHITE);
		
		new SchoolAddress().execute();
		ForgetPasswordTextview.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				ForgetpasswordFragment=new ForgetPasswordFragment();
				FragmentManager fm=getFragmentManager();
				FragmentTransaction ft=fm.beginTransaction().replace(R.id.fragment, ForgetpasswordFragment);
				ft.commit();
				//ab.setTitle("Forget Password");	
				LoginTextview.setTextColor(Color.BLACK);
				LoginTextview.setBackgroundResource(R.color.white);
				ForgetPasswordTextview.setBackgroundResource(R.color.none);
				ForgetPasswordTextview.setTextColor(Color.WHITE);
				
			}		
		});
		
LoginTextview.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				LoginFragment=new LoginPageFragment();
				FragmentManager fm=getFragmentManager();
				FragmentTransaction ft=fm.beginTransaction().replace(R.id.fragment, LoginFragment);
				ft.commit();
				LoginTextview.setBackgroundResource(R.color.none);
				ForgetPasswordTextview.setTextColor(Color.BLACK);
				ForgetPasswordTextview.setBackgroundResource(R.color.white);
				LoginTextview.setTextColor(Color.WHITE);
								
			}		
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		
		switch (item.getItemId()) 
		{
		case android.R.id.home:
			Intent rlogin=new Intent(Login.this, Login.class);
			startActivity(rlogin);
			return true;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	private class SchoolAddress extends AsyncTask<String, String,Schooldetails>
	{
		
		

		@Override
		protected Schooldetails doInBackground(String... params) {
			Schoolservice();
			// TODO Auto-generated method stub
//		
			return schooldetails;
		}
			
			protected void onPostExecute(final Schooldetails schooldetails) {
				// TODO Auto-generated method stub
				//super.onPostExecute(result);
				
			}
	}
	public void Schoolservice()
    {
        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

//   	 PropertyInfo properties=new PropertyInfo();
//    	properties.name="";
//    	properties.type=PropertyInfo.STRING_CLASS;
//    	//String v="DAV012K14LKG";
//    	request.addProperty(properties);
       SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
                SoapEnvelope.VER11);
        envelope.dotNet = true;

        envelope.setOutputSoapObject(request);

        HttpTransportSE httpTransport = new HttpTransportSE(URL);
        
        httpTransport.debug=true;
        
        try
        {	Log.d("webservice", "inside the try");
        httpTransport.call(SOAP_ACTION, envelope);
        SoapPrimitive result=(SoapPrimitive) envelope.getResponse();
        Log.i("result", result.toString());
            
            System.out.println("resulttttttttttttttt"+result);
           String bracket =result.toString();
            GsonBuilder gsonBuilder=new GsonBuilder();
            Gson gson=gsonBuilder.create();
           
			 schooldetails=gson.fromJson(bracket, Schooldetails.class);
//			if(studentProfile.getCode().equals(con.mSERVICEINVALID)){
//				codeval="444";
//				invalid(codeval);
//			}
        }
        catch (SocketException exception)
        {
            System.out.println(exception);
        } catch (IOException e) {
			e.printStackTrace();
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		}
    }


}
